package com.shandao.shangxiu.ui;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.shandao.shangxiu.R;
import com.shandao.shangxiu.base.BaseActivity;
import com.shandao.shangxiu.util.PatternCheck;
import com.shandao.shangxiu.util.TDevice;
import com.shandao.shangxiu.util.UIHelper;
import com.shandao.shangxiu.widget.CleanableEditText;

/**
 * 注册页面
 * @author king
 */
public class RegistActivity extends BaseActivity {

	private CleanableEditText et_user_phone;//手机号码输入
	private CleanableEditText et_check_code;//验证码输入
	private CleanableEditText et_user_password;//密码输入
	private TextView tv_checkcode;//获取验证码按钮
	private CheckBox cb_agree;
	private Button btn_next;
	private TextView tv_terms_service;//注册协议按钮

	private CountDownTimer timer;//倒计时

	private String checkCode;//验证码
	private String phone;//手机号码
	private String password;//手机号码

	@Override
	protected int getLayoutId() {
		return R.layout.activity_regist;
	}


	@Override
	public void initView() {
		et_user_phone = (CleanableEditText) findViewById(R.id.et_user_phone);
		et_check_code = (CleanableEditText) findViewById(R.id.et_check_code);
		et_user_password = (CleanableEditText) findViewById(R.id.et_user_password);
		tv_checkcode = (TextView) findViewById(R.id.tv_checkcode);
		cb_agree = (CheckBox) findViewById(R.id.cb_agree);
		btn_next = (Button) findViewById(R.id.btn_next);
		tv_terms_service = (TextView) findViewById(R.id.tv_terms_service);

		tv_checkcode.setOnClickListener(this);
		btn_next.setOnClickListener(this);
		tv_terms_service.setOnClickListener(this);
	}


	@Override
	public void initData() {
		setToolbarTitle("注册");
		toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem menuItem) {
				switch (menuItem.getItemId()) {
					case R.id.action_login:
						UIHelper.showLoginActivity(RegistActivity.this);
						finish();
						break;
					default:
				}
				return false;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_register, menu);
		return true;
	}

	/**
	 * 启动倒计时
	 */
	private void startCountDown() {
		tv_checkcode.setEnabled(false);
		timer = new CountDownTimer(59000, 1000) {
			public void onTick(long millisUntilFinished) {
				tv_checkcode.setText(millisUntilFinished / 1000 + "s\n重发验证码");
			}

			@Override
			public void onFinish() {
				tv_checkcode.setText("获取验证码");
				tv_checkcode.setEnabled(true);
			}
		};
		timer.start();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btn_next) {// 下一步
			next();
		} else if (id == R.id.tv_checkcode) {//验证码
			getCheckCode();
		}  else if (id == R.id.tv_terms_service) {//注册协议
//			UIHelper.openBrowser(this,);
		}
	}


	private void next() {
		if (!isPrepareForNext()) {
			return;
		}
		checkCheckCode();
	}

	private void checkCheckCode() {
		isPrepareForNet();
		// 验证验证码的正确性
		Bundle bundle = new Bundle();
		bundle.putString("phone", phone);
		bundle.putString("password", password);
		UIHelper.showSetupInformatioActivity(this, bundle);
	}


	/**
	 * 获取验证码
	 */
	public void getCheckCode() {
		if (!isPrepareForGetCheckCode()) {
			return;
		}
		tv_checkcode.setEnabled(false);
		startCountDown();
//		Api.getCheckCode(TAG, "101", phone,
//				new VolleyInterface<ResponseBase>() {
//
//					@Override
//					public void onSuccess(ResponseBase result) {
//						startCountDown();
//					}
//
//					@Override
//					public void onFailed(String errorrMsg) {
//						showToast(errorrMsg);
//					}
//				});

	}

	private boolean isPrepareForGetCheckCode() {
		isPrepareForNet();
		phone = et_user_phone.getText().toString().trim();
		if (!PatternCheck.isMobile(phone)) {
			showToast(getString(R.string.phone_error));
			return false;
		}
		return true;
	}

	private boolean isPrepareForNext() {
		if (!isPrepareForGetCheckCode()) {
			return false;
		}
		checkCode = et_check_code.getText().toString().trim();
		if (TextUtils.isEmpty(checkCode)) {
			showToast(getString(R.string.checkcode_error));
			return false;
		}
		password = et_user_password.getText().toString().trim();
		if (TextUtils.isEmpty(password) || password.length() < 8) {
			showToast(R.string.password_error);
			return false;
		}
		if(!cb_agree.isChecked()){
			showToast("请先同意服务协议");
			return false;
		}
		return true;
	}
}
